
package com.example.testandroid;

import android.view.View;
import android.widget.FrameLayout.LayoutParams;

public class ScoreCounter {
    private TargetPonitAddView mTargetView;
    private int mRingCount;

    public ScoreCounter(TargetPonitAddView targetView, int ringCount) {
        this.mTargetView = targetView;
        mRingCount = ringCount;
    }

    public static class ShootPoint {
        int x;
        int y;
        int score;

        @Override
        public String toString() {
            return "x = " + x + " y=" + y + " score=" + score;
        }
    }

    public ShootPoint getLastShootPoint() {
        return getShootPoint(mTargetView.getChildCount() - 1);
    }

    private ShootPoint getShootPoint(int index) {
        int[] center = mTargetView.getCenter();
        View item = mTargetView.getChildAt(index);
        ShootPoint point = new ShootPoint();
        LayoutParams layoutParams = (LayoutParams) item.getLayoutParams();
        point.x = layoutParams.leftMargin
                + (item.getMeasuredWidth() / 2);
        point.y = layoutParams.topMargin
                + (item.getMeasuredHeight() / 2);
        countScore(center, point);
        return point;
    }

    private void countScore(int[] center, ShootPoint point) {
        double distance = Math.sqrt(Math.pow(center[0] - point.x, 2)
                + Math.pow(center[1] - point.y, 2));
        double levelLength = mTargetView.getMeasuredWidth() / 2 / mRingCount;
        point.score = 10 - (int)(distance / levelLength) * 2;
    }

}
